package com.diandian.rabbitmq.cftutorial.simple;


import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
public class HomeController {
    //private Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Autowired
    AmqpTemplate amqpTemplate;


    @RequestMapping(value = "/")
    public String home(Model model) {
        model.addAttribute(new Message());
//        ActiveRecordConfig.UserModel userModel= ActiveRecordConfig.UserModel.dao.findFirst("select * from customer");
//        logger.info("user mobile is {}",userModel.get("mobile"));
        return "WEB-INF/views/home.jsp";
    }

    @RequestMapping(value = "/publish", method = RequestMethod.POST)
    public String publish(Model model, Message message) {
        // Send a message to the "messages" queue
        // amqpTemplate.convertAndSend("messages", message.getValue());
//        amqpTemplate.convertAndSend("errorMsgQueue", message.getValue());
    	//exchange-spring exchange id or name , routing key-spring binding key,message obj
        amqpTemplate.convertAndSend("errorMsg",message.getValue(), message.getValue());//from

        
        amqpTemplate.convertAndSend("inform",message.getValue(), message.getValue());//from
        
        
        model.addAttribute("published", true);
        return home(model);
    }

}
